package com.taxsystem.chenliangli.askquestion;

import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.main.taxsystem.R;
import com.taxsystem.chenliangli.askquestion.data.answer;
import com.taxsystem.chenliangli.askquestion_tool.HttpUtil;
import com.taxsystem.chenliangli.askquestion_tool.QuestionAnswer;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class listAllActivity extends Activity {
	
	
	private ListView  listView;
	private String url;
	private answer[] ans;
	private String[]  adapterData=null;
    public final static String RECI_COAST = "sw.system.HTTUTIL";
    private String URL= "http://mtaxtest.duapp.com/question/listall?";
    private JSONObject jsonanswer=new JSONObject();
   // ColorStateList allWhite=ColorStateList.valueOf(0xffffffff);

	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listall_question);
        init();
	}
	
	//初始化所有的组件
	public void init()
	{
		listView=(ListView)findViewById(R.id.listall_lv1);
		
		//向服务器发送请求   获得所有问题的回答
    	//QuestionAnswer sap=new QuestionAnswer();
    	//////根据style的值来判断：1:查看所有问题；   2：查询答复问题； 3：录入问题
    	//并返回搜索到的结果
    	//public  answer[]  getAnswer(int  style,String fuzzyquuery)
    	ans=getAnswer(1,"");
    	if(ans!=null)
    	{
		    	for(int i=0;i<ans.length;i++)
		    	{
		    		adapterData[i]=new String();
		    		adapterData[i]=ans[i].getTitle();
		    		
		    	}
    	}
    	
    	//为listView添加适配器 （到时候用于数据的展现）
		if( adapterData!=null)
		{
		listView.setAdapter(new ArrayAdapter<String>(listAllActivity.this, 
                android.R.layout.simple_list_item_1, adapterData));
		}
		else
		{
			adapterData=new String[]{"服务器端没有响应","有什么税务类型", "报税的流程是","如果没有按时报税怎么办","可以怎么报税  网上   大厅","在哪里查看我的报税情况","个人所得税的税务是？？"};
			
			listView.setAdapter(new ArrayAdapter<String>(listAllActivity.this, 
                R.layout.list_item1, adapterData));
		}	
		
		
		listView.setOnItemClickListener(content_item_listener);
	}

	
	
	
	//定义一个OnItemClickListener()   对象   用来为listView添加OnItemClickListener()   响应
	OnItemClickListener content_item_listener = new OnItemClickListener()     
	{   
		@Override   
        public void onItemClick(AdapterView<?> parent, View view, int position,     
                        long id)    
        {        
                
        	Intent intent =new Intent();
        	//这里到时候要改成intent.putExtra("title", ans[position].getTitle());那些！！！！！！！！
        	intent.putExtra("title", adapterData[position]);
        	intent.setClass(listAllActivity.this, searchActivity.class);
        	//执行该Intent
        	 startActivity(intent);
        	
         }
	};
	
	
	
	
	   /**********根据style的值来判断：1:查看所有问题；   2：查询答复问题； 3：录入问题
	并返回搜索到的结果**************/
	public  answer[]  getAnswer(int  style,String fuzzyquuery)
	{
		
		switch(style)
		{
		case 1:{URL=URL+"public=-1"+"&hasanswer=-1"+"fuzzyquuery="+fuzzyquuery;}break;
		case 2:{URL=URL+"public=-1"+"&hasanswer=1"+"fuzzyquuery="+fuzzyquuery;}break;
		case 3:{URL=URL+"public=-1"+"&hasanswer=-1"+"fuzzyquuery="+fuzzyquuery;}break;
		}
		
		
		try {
			
			
			//注册广播并向服务器端发送请求，将返回的数据放到jsonanseer中       
		      MyBroadcast();
			
		      JSONArray jsonArray;
			  jsonArray = jsonanswer.getJSONArray("data");
			  answer[] ans=new answer[jsonArray.length()];
				for (int i = 0; i < jsonArray.length(); i++) {
					    System.out.println("element " + i + " :" + jsonArray.get(i));
					    JSONObject j1=(JSONObject)jsonArray.get(i);
					    ans[i].setId(j1.getString("questionId"));
					    ans[i].setTitle(j1.getString("questionTitle"));
					    ans[i].setCreateTime(j1.getString("createTime"));
					    ans[i].setAnswerCount(j1.getString("answerCount"));   
			   }
				
				return ans;
			
			} catch (JSONException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			
			
			return null;
	}
	
	
	
	
	/***********注册广播的**********************/
	public void MyBroadcast()
	{
		
		// 注册广播
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil
							.getRequest(URL);
					intent.putExtra("data", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
		
	}
	
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				jsonanswer = new JSONObject(
						intent.getStringExtra("data"));
				
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}
	
	
	
	//
}
